#include <REG51.H>
sbit set = P3 ^ 2;
sbit add = P3 ^ 3;
sbit sub = P3 ^ 4;
bit key_mark, Stop;

int startmark, endmark, checkmark, sum, k;
unsigned char pos, cnt, sec, mode;
unsigned int cs[] = {0xfe, 0xfd, 0xfb, 0xf7, 0xef, 0xdf};
unsigned int nums[] = {0x3f, 0x06, 0x5b, 0x4f,
                       0x66, 0x6d, 0x7d, 0x07,
                       0x7f, 0x6f, 0x77, 0x7c,
                       0x39, 0x5e, 0x79, 0x71};
unsigned int time[6];
char second, minute, hour, uart_dat, i, BUF[10], checksum;
void delay()
{
    int i, j;
    for (i = 0; i < 120; i++)
        for (j = 0; j < 300; j++)
            ;
}
void USART_Init()
{
    TMOD = TMOD & 0X0f | 0X20; //?¤×÷·???2??×?????×°??
    TH1 = 0XFD;
    TL1 = 0XFD; //?¨????9600
    TR1 = 1;
    SCON = 0X50;
    ES = 1;
    EA = 1;
}
void sendchar(unsigned char n)
{
    SBUF = n;
    while (!TI)
        ;
    TI = 0;
}

void sendstring(unsigned char *p)
{
    while (*p)
        sendchar(*p++);
}
void crlf()
{
    sendchar(0x0d);
    sendchar(0x0a);
}

void Usart_R()
{

    switch (uart_dat)
    {
    case 'B':
        TR0 = 1;
        break;
    case 'S':
        TR0 = 0;
        break;
    case 'R':
        hour = 0;
        minute = 0;
        second = 0;
        break;
    case 'T':
        sendchar(hour / 10 + 48);
        sendchar(hour % 10 + 48);
        sendchar(':');
        sendchar(minute / 10 + 48);
        sendchar(minute % 10 + 48);
        sendchar(':');
        sendchar(second / 10 + 48);
        sendchar(second % 10 + 48);
        crlf();
        break;
    case 'G':
        hour = (BUF[2] - 48) * 10 + (BUF[3] - 48);
        minute = (BUF[4] - 48) * 10 + (BUF[5] - 48);
        second = (BUF[6] - 48) * 10 + (BUF[7] - 48);
        break;
    default:
        break;
    }
    i = 0;
}
void USART_Set() interrupt 4
{
    if (RI == 1)
    {
        RI = 0;
        switch (SBUF)
        {
        case 'B':
            startmark = 1;
            break;
        case 'E':
            endmark = 1;
            startmark = 0;
            break;
        }

        if (startmark == 1)
        {
            BUF[i++] = SBUF;
        }
        else if (endmark == 1)
        {
            endmark = 0;

            for (k = 0; k < 8; k++)
            {
                sum += BUF[k];
            }

            if (sum == BUF[i - 2])
            {
                uart_dat = BUF[1];
                Usart_R();
            }
        }
    }
}

void key()
{
    if (!set)
    {
        if (++mode > 3)
            mode = 0;
    }
    else if (!add)
    {
        if (mode == 1)
        {
            if (++hour > 23)
                hour = 0;
        }
        else if (mode == 2)
        {
            if (++minute > 59)
                minute = 0;
        }
        else if (mode == 3)
        {
            if (++second > 59)
                second = 0;
        }
    }
    else if (!sub)
    {
        if (mode == 1)
        {
            if (--hour < 0)
                hour = 23;
        }
        else if (mode == 2)
        {
            if (--minute < 0)
                minute = 59;
        }
        else if (mode == 3)
        {
            if (--second < 0)
                second = 59;
        }
    }
}
void T0Ser() interrupt 1
{
    TH0 = -5000 >> 8;
    TL0 = -5000;
    if (++pos > 5)
        pos = 0;
    if (mode == 0)
    {
        P2 = cs[pos];
        if (pos == 1 || pos == 3)
        {
            P1 = nums[time[pos]] + 128;
        }
        else
        {
            P1 = nums[time[pos]];
        }
    }
    else if (mode == 1)
    {
        if (pos == 0 || pos == 1)
        {
            P2 = cs[pos];
            P1 = nums[time[pos]];
        }
    }
    else if (mode == 2)
    {
        if (pos == 2 || pos == 3)
        {
            P2 = cs[pos];
            P1 = nums[time[pos]];
        }
    }
    else if (mode == 3)
    {
        if (pos == 4 || pos == 5)
        {
            P2 = cs[pos];
            P1 = nums[time[pos]];
        }
    }

    if (++cnt == 200)
    {
        cnt = 0;
        sec = 1;
    }
}
void InitT0()
{
    TMOD = 0x01;
    IE = 0x82;
    TR0 = 1;
}
void main()
{
    hour = 12;
    minute = 0;
    second = 0;
    InitT0();
    USART_Init();

    while (1)
    {
        if ((!set || !add || !sub) && !key_mark)
        {
            delay();
            if (!set || !add || !sub)
            {
                key_mark = 1;
                key();
                key_mark = 0;
            }
        }

        if (sec)
        {
            sec = 0;
            if (mode == 0)
            {
                if (++second > 59)
                {
                    second = 0;
                    if (++minute > 59)
                    {
                        minute = 0;
                        if (++hour > 23)
                            hour = 0;
                    }
                }
            }
        }

        time[0] = hour / 10;
        time[1] = hour % 10;
        time[2] = minute / 10;
        time[3] = minute % 10;
        time[4] = second / 10;
        time[5] = second % 10;
    }
}